#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <QDebug>
#include <QObject>

typedef enum {
    //system info
    ReadLightSourceSoftwareVersion,
    ReadMainframeMcuSoftwareVersion,
    ReadWriteLightSourceSn,
    ReadWriteMainframeSn,
    ReadWriteMirrorInfo,
    ReadWritetMainframeEepromInfo,
    ReadWriteLightSourceEepromInfo,
    Reserved01,
    Reserved02,
    Reserved03,
    Reserved04,
    //light source update
    LightSourceUpdateJump,
    LightSourceUpdateStart,
    LightSourceUpdateRequest,
    LightSourceUpdateResponse,
    LightSourceUpdateStop,
    //auto adjust light
    FpgaSyncMeteringValue,
    //mainframe mcu update
    MainframeMcuUpdateJump,
    MainframeMcuUpdateStart,
    MainframeMcuUpdateRequest,
    MainframeMcuUpdateResponse,
    MainframeMcuUpdateStop,
    //function cmd
    ReportKeyValue,
    TouchSoundBeep,
    SystemShutdown,
    DecodingDeckPowerOn,
    SwitchSpiFlash,
    StripeCalibration,
    SensorCalibration,
    OpenOrCloseAJL,//自动调光
    CurrentSensorTtype,//当前镜子类型
    SensorRegisterOperation,//读写寄存器
    SensorImageERROR,
} CommandCode;

typedef enum {
    FRAME_STATE_NOT_FIND_HEAD,
    FRAME_STATE_CRC_ERROR,
    FRAME_STATE_COMPLETE
}FrameState;


#define MAX_DATA_LENGTH			(1024)
#define FRAME_MAX_LENGTH		(MAX_DATA_LENGTH + 14)
#define FRAME_HEADER            (0x69)
#define FRAME_TAIL              (0x96)

//addr
typedef enum {
    LightSourceMcu              = 0x0001,
    MainframeMcu 				= 0x0002,
    MainframeArm 				= 0x0004,
    Fpag						= 0x0008,
    LightSourceExternalDevice	= 0x0010,
    MainframeExternalDevice		= 0x0020,
} Addr;

typedef struct {
    FrameState state;

    uint8_t head;
    uint16_t from;
    uint16_t to;
    uint16_t commandCode;
    uint16_t datalLength;
    uint8_t reserved01;
    uint8_t reserved02;
    uint8_t reserved03;
    uint8_t* data;
    uint8_t crc;
    uint8_t tail;
} Frame;

class Protocol : public QObject
{
    Q_OBJECT

public:
    static Protocol* getInstance();
    void parse(uint8_t* buffer);
    void send(Frame* frame);
private:
    Protocol();

signals:
    void parseCompleteSignal(Frame* frame);
};

#endif // PROTOCOL_H
